package com.wy.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wy.entity.GoodsOrder;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface GoodsOrderMapper extends BaseMapper<GoodsOrder> {

    /**
     * 批量插入订单信息
     * @param orders 订单集合
     * @return
     */
    @Insert("<script>" +
            "INSERT INTO goods_order" +
            "        (order_id,goods_id,seller_user_id,buyer_user_id,transaction_amount,create_time,update_time)" +
            "        VALUES" +
            "        <foreach collection ='orders' item='order' separator =','>" +
            "            (#{order.orderId},#{order.goodsId},#{order.sellerUserId},#{order.buyerUserId},#{order.transactionAmount}," +
            "              #{order.createTime},#{order.updateTime} )" +
            "        </foreach >" +
            "</script>" )
    boolean insertBatch(List<GoodsOrder> orders);

    /**
     * 更新状态  当72小时未付款
     * @return
     */
    @Update("update goods_order " +
            "set status = 3 " +
            "where `status` = 1 " +
            "and timestampdiff(hour,goods_order.create_time,sysdate()) > 72")
    int  updateStatusByTime();


    /**
     * 根据用户id 和 状态查询全部
     * @param userId 用户id
     * @param status 状态（1待付款 2已付款 3未支付 4已退款）默认1
     * @return
     */
    @Select(" SELECT goods_order.* ,goods_info.goods_name,goods_type.type_name FROM goods_order ,goods_info,goods_type\n" +
            " WHERE goods_order.buyer_user_id = #{userId}\n" +
            " AND goods_order.goods_id = goods_info.goods_id\n" +
            " AND goods_info.type_id = goods_type.type_id\n" +
            " AND goods_order.`status` = #{status}")
    List<GoodsOrder> queryAllByUserIdAndStatus(Long userId ,String status);

    /**
     * 查询已付款
     * @param userId 用户id
     * @param status 状态1未收货 2已收货

     * @return
     */
    @Select(" SELECT goods_order.* ,goods_info.goods_name,goods_type.type_name FROM goods_order ,goods_info,goods_type,goods_logistics\n" +
            " WHERE goods_order.buyer_user_id = #{userId}\n" +
            " AND goods_order.goods_id = goods_info.goods_id\n" +
            " AND goods_info.type_id = goods_type.type_id\n" +
            " AND goods_order.`status` = \"2\" "+
            "\tAND goods_logistics.order_id = goods_order.order_id\n" +
            "\tAND goods_logistics.`status` = #{status}\n"
                 )
    List<GoodsOrder> queryAllByUserIdAndStatus2(Long userId ,String status);



    /**
     * 查询卖出商品（未发货）
     * @param userId 卖方用户id
     * @return
     */
    @Select(" \n" +
            " SELECT goods_order.*,goods_info.goods_name \n" +
            " FROM goods_info,goods_order\n" +
            "WHERE goods_info.goods_id = goods_order.goods_id\t\n" +
            "and seller_user_id = #{userId}\n" +
            "AND order_id not in  (SELECT order_id FROM goods_logistics ) ")
    List<GoodsOrder> queryAllBySellerUserId(Long userId);

    /**
     * 查询卖出商品（已发货）
     * @param userId 卖方用户id
     * @return
     */
    @Select(" \n" +
            " SELECT goods_order.*,goods_info.goods_name \n" +
            " FROM goods_info,goods_order\n" +
            "WHERE goods_info.goods_id = goods_order.goods_id\t\n" +
            "and seller_user_id = #{userId}\n" +
            "AND order_id in  (SELECT order_id FROM goods_logistics where status = \"2\" ) ")
    List<GoodsOrder> queryAllBySellerUserId2(Long userId);

    /**
     * 查询卖出商品（已收货）
     * @param userId 卖方用户id
     * @return
     */
    @Select(" \n" +
            " SELECT goods_order.*,goods_info.goods_name \n" +
            " FROM goods_info,goods_order\n" +
            "WHERE goods_info.goods_id = goods_order.goods_id\t\n" +
            "and seller_user_id = #{userId}\n" +
            "AND order_id in  (SELECT order_id FROM goods_logistics where status = \"3\") ")
    List<GoodsOrder> queryAllBySellerUserId3(Long userId);

}